﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Gem_Tools_Utility.Expression_Evaluator;
namespace Gem_Tools_Utility
{
    public class CUtility
    {
        public static DateTime ConvertFromStringToDateTime(string p_strDateTime)
        {
            IFormatProvider culture = new CultureInfo("fr-FR", true);
            return DateTime.Parse(p_strDateTime, culture);
        }

        public static DateTime GetDefaultDate()
        {
            return ConvertFromStringToDateTime("01/01/1900 00:00:00");
        }

        /// <summary>
        /// Cons
        /// </summary>
        public static int Convert_To_Int32(object p_objData)
        {
            if (p_objData != System.DBNull.Value)
                return Convert.ToInt32(p_objData);
            else
                return CConst.INT_VALUE_NULL;
        }

        /// <summary>
        /// Cons
        /// </summary>
        public static double Convert_To_Double(object p_objData)
        {
            if (p_objData != System.DBNull.Value)
                return Convert.ToDouble(p_objData);
            else
                return CConst.FLT_VALUE_NULL;
        }

        
        /// <summary>
        /// Cons
        /// </summary>
        public static bool Convert_To_Bool(object p_objData)
        {
            if (p_objData != System.DBNull.Value)
                return Convert.ToBoolean(p_objData);
            else
                return CConst.BL_VALUE_NULL;
        }

        /// <summary>
        /// Cons
        /// </summary>
        public static DateTime Convert_To_DateTime(object p_objData)
        {
            if (p_objData != System.DBNull.Value && p_objData != null)
                return Convert.ToDateTime(p_objData);
            else
                return CConst.DTM_VALUE_NULL;
        }

        /// <summary>
        /// Cons
        /// </summary>
        public static string Convert_To_String(object p_objData)
        {
            return Convert.ToString(p_objData);
        }

        /// <summary>
        /// Mã hoá chuỗi (MD5)
        /// </summary>
        /// <param name="p_strSource">DataSource</param>
        /// /// <param name="p_strIT">Mã số nhận dạng</param>
        /// <returns>string</returns>
        public static string MD5_Encrypt(string p_strSource)
        {
            System.Text.UTF8Encoding utf8encoding = new System.Text.UTF8Encoding();
            byte[] Data = utf8encoding.GetBytes(p_strSource);

            System.Security.Cryptography.MD5CryptoServiceProvider md5Encrypt = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashData = md5Encrypt.ComputeHash(Data);

            string result = "";

            for (int i = 0; i < hashData.Length; i++)
                result += Convert.ToString(hashData[i], 16).PadLeft(2, 'j');

            return result.PadLeft(32, 'n');
        }

        public static DateTime Add_Day_Include_Saturday(DateTime p_dtmNow, int p_iDay)
        {
            int v_iCount = 0;
            int v_iSub = 1;
            if (p_iDay < 0)
                v_iSub = -1;
            DateTime v_dtRes = p_dtmNow;

            while (v_iCount < Math.Abs(p_iDay))
            {
                v_iCount++;
                v_dtRes = v_dtRes.AddDays(v_iSub);

                while (v_dtRes.DayOfWeek == DayOfWeek.Sunday)
                    v_dtRes = v_dtRes.AddDays(v_iSub);
            }

            return v_dtRes;
        }

        // hàm tính kết quả của 1 biếu thức:
        public static double Caculated_Expression(string p_strExpression)
        {
            ExpressionEval v_objEval = new ExpressionEval();
            v_objEval.Expression = p_strExpression;
            return v_objEval.EvaluateDouble();
        }
    }
}
